<!DOCTYPE html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<jsp:include page="/common/commonCss.jsp" />
<jsp:include page="/common/commonJs.jsp" />
<script src="${basePath}commonJs/jquery.imageScroller.js"></script>
<script type="text/javascript">

	// 特殊折扣选择行
	var row;

	// 计算商品价格
	function calculatePrice() {
		var orderTotal = 0;
		var orderLen = "${fn:length(orderList)}";
		for (var i = 1; i <= orderLen; i++) {
			
			var total = parseFloat($("#tdTotal" + i).html());
			var discount = parseFloat($("#discount" + i).val());
			var discountTotal = (isNaN(discount) ? total : total * discount);
			discountTotal = parseFloat(discountTotal.toFixed(2));
			$("#tdDiscountTotal" + i).html(discountTotal);
			$("#discountTotal" + i).val(discountTotal);
			orderTotal += discountTotal;
		}
		//订单总额
		$("#orderTotal").html(orderTotal);
		// 礼券
		var giftTotal = 0;
		$("#tabGift tbody tr td::nth-child(3)").each(function(index, item) {
			giftTotal += isNaN(parseFloat(this.innerHTML)) ? 0 : parseFloat(this.innerHTML);
		});
		$("#giftTotal").html(giftTotal * -1);
		// 应付总额
		$("#spAmount").html(orderTotal + giftTotal * -1);
		$("#amount").val(orderTotal);
	}
	
	$(document).ready(function(){
		
		calculatePrice();
		
		// 特殊折扣【确定】
		$("#btnDiscountOK").click(function() {
			if ($("#pwd").val() == "") {
				alertError("经理密码不能为空");
				return false;
			}
			// 经理密码验证
			$.ajax({
				cache : false,
				type : "get",
				url : "${basePath}membership/single/validationManagerPassword.shtml",
				data : "password=" + $("#pwd").val(),
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				},
				success : function(msg) {
					if (msg == "0") {
						alertError("密码错误！");
						return false;
					}
					
					$("#tdDiscount" + row).html($("#selDis  option:selected").text());

					var pkDisId = $("#selDis").val().split("_")[0];
					var discount = $("#selDis").val().split("_")[1];
					$("#pkDisId" + row).val(pkDisId);
					$("#discount" + row).val(discount);
					$("#fkUsrId" + row).val(msg);
					
					calculatePrice();
					$('#popDiscount').window('close');
				},
				error : function() {
					alertError("系统异常!");
				}
			});
		});

		// 下单结算
		$("#placeOrder").click(function() {
			var overMoney = parseFloat($("#spAmount").html()) < 0 ? "礼券面额大于订单总额，" : "";
			$.messager.confirm('提示', overMoney + '确认要下单吗?', function(r) {
				if (r) {
					$.ajax({
						cache : false,
						type : "post",
						url : "${basePath}membership/member/placeOrder.shtml",
						data : $("#orderForm").serialize(),
						success : function(msg) {
							if (msg != null) {
								$("#orderId").val(msg);
								$('#placeOrder').attr('disabled',"true");
								$('#placeOrder').removeClass().addClass("btn_no");
								$('#btnPre').attr('disabled',"true");
								$('#btnPre').removeClass().addClass("btn_no");
								alertInfo("订单保存成功");
							}
						},
						error : function(XMLHttpRequest) {
							ajaxError(XMLHttpRequest);
						}
					});
				}
			});
		});

		// 刷新付款结果
		$("#refreshPayResult").click(function() {
			$.ajax({
				cache : false,
				type : "post",
				url : "${basePath}membership/member/refreshPayResult.shtml",
				data : "orderId=" + $("#orderId").val(),
				success : function(data) {
					if (data == 1) {
						$("#payId").val("已付款");
					} else {
						$("#payId").val("未付款");
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		});

		// 验证礼券
		$("#btnValidation").click(function() {
			if ($("#giftNo").val() == "") {
				alertError("请输入礼券编号！");
				return false;
			}
			var giftNo = $("#selGift").val().split(",")[0];
			var exsit = true;
			$("#tabGift tbody tr td::nth-child(1) input[name='giftNos']").each(function(index, item){
				if (item.value == (giftNo + "_" +  $("#giftNo").val())) {
					exsit = false;
					return;
				}
			});
			if (!exsit) {
				alertError("该礼券已存在于列表中，不能重复！");
				return false;
			}
			$.ajax({
				cache : false,
				type : "post",
				url : "${basePath}membership/member/validationGiftNo.shtml",
				data : "giftNo=" +  (giftNo + "_" + $("#giftNo").val()),
				success : function(data) {
					if (data == 1) {
						alertInfo("可以使用");
					} else {
						alertError("该礼券已被使用！");
						return false;
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		});
		
		// 激活礼券
		$("#btnActive").click(function() {
			if ($("#giftNo").val() == "") {
				alertError("请输入礼券编号！");
				return false;
			}
			var giftNo = $("#selGift").val().split(",")[0];
			var giftPrice = $("#selGift").val().split(",")[1];
			var exsit = true;
			$("#tabGift tbody tr td::nth-child(1) input[name='giftNos']").each(function(index, item){
				if (item.value == (giftNo + "_" + $("#giftNo").val())) {
					exsit = false;
					return;
				}
			});
			if (!exsit) {
				alertError("该礼券已存在于列表中，不能重复！");
				return false;
			}
			$.ajax({
				cache : false,
				type : "post",
				url : "${basePath}membership/member/validationGiftNo.shtml",
				data : "giftNo=" +  (giftNo + "_" + $("#giftNo").val()),
				success : function(data) {
					if (data == 1) {
						$("#tabGift").find("tr:not(:first)").remove();
						var tr = '<tr>';
						tr += '<td><input type="hidden" name="giftNos" value="' + giftNo + "_" + $("#giftNo").val() + '">' + $("#selGift option:selected").text() + '</td>';
						tr += '<td>' + $("#giftNo").val() + '</td>';
						tr += '<td>' + giftPrice + '</td>';
						tr += '<td><input type="button" class="btn_l" value="取消" onclick="$(this).parent().parent().remove();calculatePrice();"></td>';
						tr += '</tr>';
						$("#tabGift").append(tr);
						calculatePrice();
						$("#giftNo").val("");
					} else {
						alertError("该礼券已被使用！");
						return false;
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		});
		
	});
</script>


<title>首页</title>
<body class="bg">

	<div class="content m_auto ">
		<div class="content_l mt10">
			<ul>
				<c:if test="${type!=null && type!=''}">
				<shiro:hasPermission name="filemanagement/listVip.shtml?type=1">
					<li <c:if test="${type=='1'}">class="content_hover"</c:if>><a href="${basePath }filemanagement/listVip.shtml?type=1"><i class="nav_icon_b"></i><br>会员管理</a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="filemanagement/viewVistors.shtml?type=3">
            		<li <c:if test="${type=='3'}">class="content_hover"</c:if>><a href="${basePath }filemanagement/viewVistors.shtml?type=3"><i class="nav_icon_d"></i><br>访客管理</a></li>
            	</shiro:hasPermission>
            	<shiro:hasPermission name="filemanagement/listViewSingelTicket.shtml?type=2">
           			<li <c:if test="${type=='2'}">class="content_hover"</c:if>><a href="${basePath }filemanagement/listViewSingelTicket.shtml?type=2"><i class="nav_icon_a"></i><br>非会员管理</a></li>
           		</shiro:hasPermission>
           		<shiro:hasPermission name="parreserve/parReserveManages.shtml?indexflag=">
					<li><a href="${basePath }parreserve/parReserveManages.shtml?indexflag="><i class="nav_icon_g"></i><br>预约管理</a></li>
				</shiro:hasPermission>
           		<shiro:hasPermission name="filemanagement/customer/serviceList.shtml">
            		<li><a href="${basePath}filemanagement/customer/serviceList.shtml"><i class="nav_icon_h"></i><br>客户服务</a></li>
            	</shiro:hasPermission>
           		<shiro:hasPermission name="crm/jumpCrm.shtml">
					<li><a href="${basePath }crm/jumpCrm.shtml"><i class="nav_icon_n"></i><br>客户关系管理</a></li>
				</shiro:hasPermission>
				</c:if>
				<c:if test="${type==null || type==''}">
					<shiro:hasPermission name="membership/single/jumpSingleTicket.shtml">
			        	<li ><a href="${basePath }membership/single/jumpSingleTicket.shtml"><i class="nav_icon_a"></i><br>单次票</a></li>
					</shiro:hasPermission>
					<shiro:hasPermission name="membership/member/jumpMember.shtml">
						<li class="content_hover"><a href="${basePath }membership/member/jumpMember.shtml"><i class="nav_icon_b"></i><br>会籍</a></li>
					</shiro:hasPermission>
					<shiro:hasPermission name="membership/visitor/jumpVisitor.shtml">
						<li><a href="${basePath }membership/visitor/jumpVisitor.shtml"><i class="nav_icon_d"></i><br>访客</a></li>
					</shiro:hasPermission>
					<shiro:hasPermission name="parreserve/loadParReserve.shtml">
						<li><a href="${basePath }parreserve/loadParReserve.shtml"><i class="nav_icon_g"></i><br>派对/团体预约</a></li>
			        </shiro:hasPermission>
				</c:if>
			</ul>
		</div>
		<div class="content_r mt10">
			<div class="collection_h1 fixed">
				<ul class="order">
					<li class="order_b"><i class="order_icon_hui">1</i>信息采集</li>
					<li class="order_a"><i class="order_icon">2</i>下单</li>
				</ul>
			</div>

			<div class="payment_l mt30 tc">
				<input type="button" value="打印合同" class="btn">
			</div>
			<form id="orderForm" method="post">
			<div class="mt30 payment_rr" style="width:830px !important;">
				<input type="hidden" id="orderId" name="orderId">
				<input type="hidden" id="memberId" name="memberId"  value="${memberId}">
				<input type="hidden" id="amount" name="amount" >
				<table class="table" id="tblOrder">
					<tr>
						<th>序号</th>
						<th>姓名</th>
						<th>商品编码</th>
						<th>类型</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>小计</th>
						<th>折扣</th>
						<!-- <th>特殊折扣</th> -->
						<th>折后金额</th>
					</tr>
					<c:forEach items="${orderList}" var="product" varStatus="status">
					<tr>
						<td>${status.count }</td>
						<td title="${product. memName}">
						<c:choose> 
							<c:when test="${fn:length(product.memName) > 5}">${fn:substring(product.memName, 0, 5)}...</c:when> 
							<c:otherwise>${product.memName}</c:otherwise>
						</c:choose>
						</td>
						<td>${product.pkPrdId }</td>
						<td>会籍</td>
						<td>${product.prdName }</td>
						<td><c:if test="${product.pkPrdId ==null}">-</c:if><c:if test="${product.pkPrdId !=null}">${product.quantity }</c:if></td>
						<td><c:if test="${product.pkPrdId ==null}">-</c:if><c:if test="${product.pkPrdId !=null}">${product.productPrice }</c:if></td>
						<td id="tdTotal${status.count }">${product.productPrice * product.quantity}</td>
						<td id="tdDiscount${status.count }">
							<c:choose>
								<c:when test="${product.pkDisId == null}">无</c:when>
								<c:otherwise>${product.disDesc }</c:otherwise>
							</c:choose>
							<input type="hidden" name="fkMemId" value="${product.fkMemId }">
							<input type="hidden" name="pkPrdId" value="${product.pkPrdId }">
							<input type="hidden" name="quantity" value="${product.quantity }">
							<input type="hidden" id="pkDisId${status.count }" name="pkDisId" value="${product.pkDisId }">
							<input type="hidden" id="discount${status.count }" name="discount" value="${product.disValue }">
							<input type="hidden" id="fkUsrId${status.count }" name="fkUsrId">
							<input type="hidden" id="total${status.count }" name="total" value="${product.productPrice * product.quantity}">
							<input type="hidden" id="price${status.count }" name="price" value="${product.productPrice}">
							<input type="hidden" id="discountTotal${status.count }" name="discountTotal">
						</td>
						<!-- <td>
							<input type="button" class="btn_l" value="设置"  onclick="$('#popDiscount').window('open');row='${status.count }'; ">
						</td>-->
						<td id="tdDiscountTotal${status.count }">${product.productPrice * product.quantity}
							<c:choose>
								<c:when test="${product.pkDisId == null}">
									${product.productPrice * product.quantity}
								</c:when>
								<c:otherwise>
									${product.productPrice * product.quantity*product.disValue}
								</c:otherwise>
							</c:choose>
						</td>
					</tr>
					</c:forEach>
					<tr>
						<td colspan="10">
							<ul class="goods_ul fr p10">
								<li>订单总额：<span class="color_lv mlr5 f18">￥<span id="orderTotal"></span></span></li>
								<li>礼卷：<span class="color_lv mlr5 f18">￥<span id="giftTotal"></span></span></li>
								<li>应付金额<span class="fb mlr5 color_org f18">￥<span id="spAmount"></span></span></li>
							</ul>
						</td>
					</tr>
				</table>
			</div>
			<div class="pay_cen">
				<dl class="payment_r_dl f14">
					<dd>
						<input type="button" class="btn" value="下单结算" id="placeOrder">
						<c:if test="${giftEnable != '0' }">
						<div class="uboxstyle ml10">
				                        <select name="selGift" id="selGift">
								<c:forEach items="${giftList}" var="gift">
									<option value="${gift.pkPrdId},${gift.productPrice}">${gift.prdName}</option>
								</c:forEach>
				                        </select>
						</div>
			                    <!----select--->
			                	<span class="ml10">礼券：</span>
						<input type="text" class="iput_t" id="giftNo"><input type="button" class="btn_l ml10" id="btnValidation" value="验证"><input type="button" class="btn_l ml10" id="btnActive" value="激活">
					</dd>
					<dd>
						<table class="table" id="tabGift">
							<tr>
								<th>类型</th>
								<th>礼券</th>
								<th>面额</th>
								<th>操作</th>
							</tr>
						</table>
					</c:if>
					</dd>
					<dd class='<c:if test="${rechargeFlag != '1'}">mt40</c:if>'>
						<input type="button" class="btn w150" value="刷新付款结果" id="refreshPayResult">
						<input type="text" class="iput_t ml10" id="payId">
					</dd>
				</dl>
			</div>
			</form>


			<div class="clear"></div>
			<div class="btm_btn mt30">
				<dl>
					<dd>
						<a><input type="button" class="btn" value="上一步" id="btnPre" onclick="history.back(-1);"></a>
					</dd>
					<dd>
						<a href='<c:if test="${type!=null && type!=''}">${basePath}filemanagement/listVip.shtml</c:if><c:if test="${type==null || type==''}">jumpMember.shtml</c:if>'><input type="button" class="btn" value="完成"></a>
					</dd>
				</dl>
			</div>
		</div>
	</div>

	<!---弹出层--->
	<div id="popDiscount" class="easyui-window" title="特殊折扣设置"
		data-options="modal:true,closed:true,iconCls:'icon-save'"
		style="width: 400px; height: 200px; position: relative;">
		<div class="editor_h1 fixed">
			<div class="editor fl">
				<ul>
					<li><label>经理密码：</label><input type="password" id="pwd" class="iput_t w200"></li>
					<li>
						<label>折扣率：</label>
						<div class="uboxstyle">
							<select name="selDis" id="selDis">
								<c:forEach items="${discountList}" var="dc">
									<option value="${dc.pkDisId}_${dc.disValue}">${dc.disDesc}</option>
								</c:forEach>
							</select>
						</div>
					</li>
				</ul>
			</div>
		</div>
		<div class="btm_btn btm_bg">
			<dl>
				<dd>
					<input type="button" class="btn" id="btnDiscountOK" value="确定">
				</dd>
				<dd>
					<input type="button" class="btn" onclick="$('#popDiscount').window('close')" value="取消">
				</dd>
			</dl>
		</div>
	</div>
	<!---弹出层--->
</body>
</html>
















